import 'package:fstudy/src/config/app_config.dart';

class ConfigReader {
  static late AppConfig _config;

  static void initialize() {
    _config = AppConfig.fromEnv();
    _fetchFromPredefine();
  }

  static T get<T>(String key) {
    switch (key) {
      case 'aiApiUrl':
        return _config.aiApiUrl as T;
      case 'aiApiKey':
        return _config.aiApiKey as T;
      case '_config':
        return _config as T;
      case 'deviceId':
        return _config.deviceId as T;
      case 'conversationId':
        return _config.conversationId as T;
      default:
        throw ArgumentError('Unknown config key: $key');
    }
  }

  // 兜底
  static void _fetchFromPredefine() {
    if (_config.aiApiKey.isEmpty) {
      _config.aiApiKey = String.fromEnvironment('AI_API_KEY');
    }
    if (_config.aiApiUrl.isEmpty) {
      _config.aiApiUrl = String.fromEnvironment('AI_API_URL');
    }
  }
}
